package com.gxa.worker.recycling.controller;

import com.gxa.core.recycling.anno.NeedToken;
import com.gxa.core.recycling.common.ExceptionCode;
import com.gxa.core.recycling.common.JsonResult;
import com.gxa.core.recycling.exception.BusinessException;
import com.gxa.core.recycling.pojo.*;
import com.gxa.core.recycling.exception.BusinessException;
import com.gxa.worker.recycling.service.GoodsOrderService;
import com.gxa.worker.recycling.service.GoodsService;
import com.gxa.worker.recycling.service.RecycleorderService;
import io.swagger.annotations.*;
import org.omg.CORBA.LongHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author Bei
 * @date 2021/12/2 16:54
 */
@CrossOrigin
@RestController
@RequestMapping("/recy")
@Api("预约回收订单接口")

public class RecycleorderController {


    @Autowired
    RecycleorderService recycleorderService;

    @NeedToken
    @PostMapping("/selectByWarehoused")
    @ApiOperation("查询已入库订单信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "rid", value = "回收员id", required = true, dataType = "Long")
    })
    public Map selectByWarehoused(Long rid){
        List<Map> maps = recycleorderService.selectByWarehoused(rid);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setData(maps);
        return jsonResult.getDataMap();
    }

    /**
     * 杨述波
     * @param id
     * @return
     */
    @NeedToken
    @PostMapping("/getOrderDeepInfo")
    @ApiOperation("查询订单相关详细信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "回收订单id", required = true, dataType = "long")
    })
    public Map getOrderDeepInfo(Long id){
        Map<String, Object> maps = recycleorderService.getOrderDeepInfoById(id);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setData(maps);
        return jsonResult.getDataMap();
    }


    /**
     * 杨述波
     * @return
     */
    @NeedToken
    @PostMapping("/finishOrder")
    @ApiOperation("完成普通/临创用户回收预约单")
    //Long orderId, Long userId, Long recyclerId,String[] item
    public Map finishOrder(@RequestBody RecyclerOrderVo vo) throws BusinessException {
        System.out.println(vo);
        if(vo.getOrderId()==null ) {
            if (vo.getUserId()==null) {
                throw new BusinessException(ExceptionCode.ORDER_ERROR);
            }
        }
        if(vo.getUserId()==null ) {
            if (vo.getOrderId()==null) {
                throw new BusinessException(ExceptionCode.ORDER_ERROR);
            }
        }
        if (vo.getRecyclerId()==null) {
            throw new BusinessException(ExceptionCode.ORDER_ERROR);
        }
        System.out.println(vo);
        recycleorderService.finishOrder(vo.getOrderId(),vo.getUserId(),vo.getRecyclerId(),vo.getItem());
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("回收成功");
        return jsonResult.getDataMap();
    }




    /**
     * 廖泓宇
     * 查询回收订单状态为待回收
     * @return
     */
    @PostMapping("/selectIngOreder")
    @ApiOperation("查询待回收订单信息")
    @NeedToken
    @ApiImplicitParams({
            @ApiImplicitParam(name = "rid", value = "回收员id", required = true, dataType = "Long")
    })
    public Map selectIngOreder(Long rid){
        List<Map> maps = recycleorderService.selectIngOreder(rid);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setData(maps);
        return jsonResult.getDataMap();
    }

    /**
     * 廖泓宇
     * 查询回收订单状态为待入库
     * @return
     */
    @PostMapping("/selectOverOrder")
    @ApiOperation("查询待入库订单信息")
    @NeedToken
    @ApiImplicitParams({
            @ApiImplicitParam(name = "rid", value = "回收员id", required = true, dataType = "Long")
    })
    public Map selectOverOrder(Long rid){
        List<Map> maps = recycleorderService.selectOverOrder(rid);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setData(maps);
        return jsonResult.getDataMap();
    }

    /**
     * 廖泓宇
     * 查询回收订单状态为新订单
     * @return
     */
    @PostMapping("/selectNewOrder")
    @ApiOperation("查询待接单的订单信息")
    @NeedToken
    @ApiImplicitParams({
            @ApiImplicitParam(name = "rid", value = "回收员id", required = true, dataType = "Long")
    })
    public Map selectNewOrder(Long rid){
        List<Map> maps = recycleorderService.selectNewOrder(rid);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setData(maps);
        return jsonResult.getDataMap();
    }

    /**
     * 廖泓宇
     * 查询回收订单状态为已取消
     * @return
     */
    @PostMapping("/selectCancelOrder")
    @ApiOperation("查询已取消订单信息")
    @NeedToken
    @ApiImplicitParams({
            @ApiImplicitParam(name = "rid", value = "回收员id", required = true, dataType = "Long")
    })
    public Map selectCancelOrder(Long rid){
        List<Map> maps = recycleorderService.selectCancelOrder(rid);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setData(maps);
        return jsonResult.getDataMap();
    }

    /**
     * 廖泓宇
     * 上门称重按钮
     */
    @PostMapping("/doorToDoorWeighing")
    @ApiOperation("上门称重按钮接口")
    @NeedToken
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "回收订单id", required = true, dataType = "long"),
            @ApiImplicitParam(name = "code", value = "取件码", required = true, dataType = "String")
    })
    public Map doorToDoorWeighing(@NotNull @RequestParam Long id,String code) throws BusinessException {
        Recycleorder recycleorder = new Recycleorder();
        recycleorder.setId(id);
        recycleorder.setCode(code);
        recycleorderService.DoorToDoorWeighing(recycleorder);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        return jsonResult.getDataMap();
    }

    /**
     * 廖泓宇
     * 接单按钮
     */
    @PostMapping("/OrderReceiving")
    @ApiOperation("接单按钮接口")
    @NeedToken
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "回收订单id", required = true, dataType = "Long")
    })
    public Map OrderReceiving(@NotNull @RequestParam Long id) throws BusinessException {
        Recycleorder recycleorder = new Recycleorder();
        recycleorder.setId(id);
        recycleorderService.OrderReceiving(recycleorder);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.getDataMap();
        return jsonResult.getDataMap();
    }
    @Autowired
    GoodsOrderService goodsOrderService;

    /**
     * 廖泓宇
     * 统计待接单的记录数
     */
    @PostMapping("/countNewOrder")
    @ApiOperation("回收员端五个统计单数的接口")
    @NeedToken
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "回收员id", required = true, dataType = "Long")
    })
    public Map countNewOrder(Long id){
        Long hid = recycleorderService.villAgeId(id);
        Map map = new HashMap();
        Integer integer = recycleorderService.countNewOrder(hid);
        Integer integer1 = recycleorderService.countIngOrder(hid);
        Integer integer2 = recycleorderService.countYiRuOrder(hid);
        Integer integer3 = goodsOrderService.countNoDelivered(id);
        Integer integer4 = goodsOrderService.countDelivered(id);
        Map<String, Object> map1 = recycleorderService.imgAndAddress(id);
        map.put("daiJieDan",integer);
        map.put("daiHuiShou",integer1);
        map.put("yiRuKu",integer2);
        map.put("daiPeiSong",integer3);
        map.put("yiPeiSong",integer4);
        map.put("imgAndAddress",map1);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setData(map);
        return jsonResult.getDataMap();
    }

//    /**
//     * 廖泓宇
//     * 统计待回收的记录数
//     */
//    @PostMapping("/countIngOrder")
//    @ApiOperation("回收员端统计待回收的单数")
//    @NeedToken
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "id", value = "回收员id", required = true, dataType = "Long")
//    })
//    public Map countIngOrder(Long id){
//        Long hid = recycleorderService.villAgeId(id);
//
//        JsonResult jsonResult = new JsonResult();
//        jsonResult.setCode("200");
//        jsonResult.setData(integer);
//        return jsonResult.getDataMap();
//    }

//    /**
//     * 廖泓宇
//     * 统计已入库的记录数
//     */
//    @PostMapping("/countYiRuOrder")
//    @ApiOperation("回收员端统计已入库的单数")
//    @NeedToken
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "id", value = "回收员id", required = true, dataType = "Long")
//    })
//    public Map countYiRuOrder(Long id){
//        Long hid = recycleorderService.villAgeId(id);
//
//        JsonResult jsonResult = new JsonResult();
//        jsonResult.setCode("200");
//        jsonResult.setData(integer);
//        return jsonResult.getDataMap();
//    }

    /**
     * 廖泓宇
     * 问题反馈提交
     */
    @PostMapping("/problemFeedback")
    @ApiOperation("问题反馈提交接口")
    @NeedToken
    @ApiImplicitParams({
            @ApiImplicitParam(name = "image", value = "图片路径", required = true,dataType = "String"),
            @ApiImplicitParam(name = "words", value = "反馈内容", required = false,dataType = "String"),
            @ApiImplicitParam(name = "hid", value = "回收员id", required = true,dataType = "Long")
    })
        public Map problemFeedback(@RequestBody AdviceVo adviceVo){
        System.out.println(adviceVo.getWords());
        System.out.println("vo:"+adviceVo.getImage());
          List<String> list = Arrays.stream(adviceVo.getImage()).collect(Collectors.toList());
        System.out.println(list);
//        System.out.println("list:"+list.size());
//        System.out.println("list2:"+list);
        while (list.size() < 3){
            list.add(null);
        }
        final String s = "https://bucket-luoxuefeng.oss-cn-chengdu.aliyuncs.com/";

        for (int i = 0; i < list.size(); i++) {
            if (list.get(i) != null){
                StringBuilder stringBuilder = new StringBuilder();
                stringBuilder.append(s).append(list.get(i));
                list.set(i, String.valueOf(stringBuilder));
                System.out.println("buileder:"+stringBuilder);
            }
        }
        System.out.println("list1:"+list);

            recycleorderService.problemFeedback(list,adviceVo.getWords(),adviceVo.getHid());



            JsonResult jsonResult = new JsonResult();
            jsonResult.setCode("200");
            return jsonResult.getDataMap();
    }

    /**
     * 廖泓宇
     * 取消订单按钮
     */
    @PostMapping("/CancellationOrder")
    @ApiOperation("取消订单接口")
    @NeedToken
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "回收订单id", required = true, dataType = "Long")
    })
    public Map CancellationOrder(Long id) throws BusinessException {
        Recycleorder recycleorder = new Recycleorder();
        recycleorder.setId(id);
        recycleorder.setState("已取消");
        recycleorderService.CancellationOrder(recycleorder);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        return jsonResult.getDataMap();
    }


}
